package Ants.Agent;

import java.util.Date;

import org.apache.log4j.Logger;

import Ants.Actuator;
import Ants.Agent.Modules.AlertModule;
import Ants.Agent.Modules.ExploreModule;
import Ants.Agent.Modules.FightModule;
import Ants.Agent.Modules.FoodModule;
import Ants.Agent.Modules.TargetModule;
import Ants.Model.Status;

public class Agent implements IAgent{

	private static Logger Log = Logger.getLogger(Agent.class);
	
	private FoodModule foodModule;
	private ExploreModule exploreModule;
	private TargetModule targetModule;
	private AlertModule alertModule;
	private FightModule fightModule;

	public Agent(Status status){		
		foodModule = new FoodModule(status);
		exploreModule = new ExploreModule(status);
		targetModule = new TargetModule(status);
		alertModule = new AlertModule(status);
		fightModule = new FightModule(status);
	}
	
	@Override
	public void setup(Status status) {		
		Actuator.EndTurn();
	}

	@Override
	public void doTurn(Status status) {
		Log.debug(String.format("Turn %s started...", status.getTurn()));
		long startTime = new Date().getTime();
		doStrategy(status);
		Actuator.EndTurn();
		long time = (new Date().getTime()) - startTime;
		tot+= time;
		if(time > max)
			max = time;
		Log.debug(String.format("Turn %s completed in %s ms, Avg %s ms, Max %s ms", status.getTurn(),(new Date().getTime()) - startTime, (tot / status.getTurn()), max));
	}
	
	//Solo per timing
	private long tot = 0;
	private long max = 0;
	
	private int alertAvg = 0;
	private int fightAvg = 0;
	private int foodAvg = 0;
	private int backupAvg = 0;
	private int exploreAvg = 0;
	private int targetAvg = 0;
	
	private long alertMax = 0;
	private long fightMax = 0;
	private long foodMax = 0;
	private long backupMax = 0;
	private long exploreMax = 0;
	private long targetMax = 0;

	private void doStrategy(Status status){
		long startTime = new Date().getTime();
		long time = 0;
		alertModule.check();
		time = new Date().getTime() - startTime;
		alertAvg += time;
		if(time > alertMax)
			alertMax = time;
		Log.debug(String.format("\talertModule completed in %s ms, avg %s ms, max %s ms",(new Date().getTime()) - startTime, alertAvg/status.getTurn(), alertMax));
		
		startTime = new Date().getTime();
		fightModule.fight();
		time = new Date().getTime() - startTime;
		fightAvg += time;
		if(time > fightMax)
			fightMax = time;
		Log.debug(String.format("\tfightModule completed in %s ms, avg %s ms, max %s ms",(new Date().getTime()) - startTime, fightAvg/status.getTurn(), fightMax));
		
		startTime = new Date().getTime();
		foodModule.gatherFood();
		time = new Date().getTime() - startTime;
		foodAvg += time;
		if(time > foodMax)
			foodMax = time;
		Log.debug(String.format("\tfoodModule completed in %s ms, avg %s ms, max %s ms",(new Date().getTime()) - startTime, foodAvg/status.getTurn(), foodMax));
		
		startTime = new Date().getTime();
		targetModule.doBackup();
		time = new Date().getTime() - startTime;
		backupAvg += time;
		if(time > backupMax)
			backupMax = time;
		Log.debug(String.format("\tdoBackup completed in %s ms, avg %s ms, max %s ms",(new Date().getTime()) - startTime, backupAvg/status.getTurn(), backupMax));
		
		startTime = new Date().getTime();
		exploreModule.explore();
		time = new Date().getTime() - startTime;
		exploreAvg += time;
		if(time > exploreMax)
			exploreMax = time;
		Log.debug(String.format("\texploreModule completed in %s ms, avg %s ms, max %s ms",(new Date().getTime()) - startTime, exploreAvg/status.getTurn(), exploreMax));

		startTime = new Date().getTime();
		targetModule.buildTarget();
		time = new Date().getTime() - startTime;
		targetAvg += time;
		if(time > targetMax)
			targetMax = time;
		Log.debug(String.format("\tbuildTarget completed in %s ms, avg %s ms, max %s ms",(new Date().getTime()) - startTime, targetAvg/status.getTurn(), targetMax));
	
		Log.debug(String.format("\tSTATUS: myAnts %s, enemies %s",status.getMyAnts().size(), status.getEnemiesAnts().size()));
	}
	
}
